/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package skyproc;

import java.io.IOException;
import java.util.zip.DataFormatException;
import lev.LExporter;
import lev.LStream;
import skyproc.exceptions.BadParameter;
import skyproc.exceptions.BadRecord;

/**
 *
 * @author Justin Swanson
 */
class SubInt extends SubRecord {

    private Integer data;
    int length = 4;

    SubInt(Type type) {
	super(type);
    }

    SubInt(Type type, int length) {
	this(type);
	this.length = length;
    }

    @Override
    SubRecord getNew(Type type) {
	return new SubInt(type);
    }

    @Override
    int getContentLength(Mod srcMod) {
	return length;
    }

    public void set (int in) {
	data = in;
        if (logging()) {
            logSync(toString(), "Setting " + toString() + " to " + print());
        }
    }

    public int get () {
	return data;
    }
    
    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final SubInt other = (SubInt) obj;
        if (this.data != other.data) {
            return false;
        }
        if (this.length != other.length) {
            return false;
        }
        return true;
    }

    @Override
    public int hashCode() {
        int hash = 5;
        hash = 67 * hash + this.data;
        hash = 67 * hash + this.length;
        return hash;
    }

    /*
     * SkyBash methods.
     */
    /**
     * Merges straight SubInts with logging capabilities.
     *
     * @param no The new SubInt to be merged.
     * @param bo The base SubInt, to prevent base data from being
     * re-merged.
     * @return The modified SubInt.
     */
    @Override
    public SubRecord merge(SubRecord no, SubRecord bo) {
        SubInt integer = this;
        if (!(no == null && bo == null && (no instanceof SubInt) && (bo instanceof SubInt))) {
            final SubInt newint = (SubInt) no;
            final SubInt baseint = (SubInt) bo;
            if (!integer.equals(newint) && !newint.equals(baseint)) {
                integer = newint;
                if (SkyBash.fullLogging) {
                    SkyBash.logMerge(type[0].toString(), integer.toString());
                }
            }
        }
        return integer;
    }

    @Override
    void parseData(LStream in) throws BadRecord, DataFormatException, BadParameter, IOException {
	super.parseData(in);
	data = in.extractInt(length);
	if (logging()) {
	    logSync(toString(), "Setting " + toString() + " to : " + print());
	}
    }

    @Override
    public String print() {
	if (isValid()) {
	    return String.valueOf(data);
	} else {
	    return super.toString();
	}
    }

    @Override
    void export(LExporter out, Mod srcMod) throws IOException {
	if (isValid()) {
	    super.export(out, srcMod);
	    out.write(data, length);
	}
    }

    @Override
    Boolean isValid() {
	return data != null;
    }
}
